﻿using Avalonia.Controls;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Collections.ObjectModel;
using System.Windows.Input;

namespace MyTodolist.ViewModels;

public partial class TodoListItem : ViewModelBase
{
    #region props

    [ObservableProperty]
    private int _index;

    [ObservableProperty]
    private string _content;

    public ICommand DeleteCommand { get; internal set; }

    #endregion
}

public partial class MainViewModel : ViewModelBase
{
    #region props

    [ObservableProperty]
    private ObservableCollection<TodoListItem> _todoList = new ObservableCollection<TodoListItem>();

    #endregion

    public MainViewModel()
    {
        TodoList.CollectionChanged += TodoList_CollectionChanged;
   
    
    }

    private void TodoList_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        for (int i = 0; i < TodoList.Count; i++)
        {
            TodoList[i].Index = i + 1;
        }
    }

    #region methods

    protected TodoListItem CreateTodoListItem(string content)
    {
        var todoListItem = new TodoListItem()
        {
            Content = content,
        };
        todoListItem.DeleteCommand = new RelayCommand(() =>
        {
            TodoList.Remove(todoListItem);
            todoListItem.DeleteCommand = null;
        });

        return todoListItem;
    }

    #endregion

    #region commands

    [RelayCommand]
    private void AddItem()
    {
        var todoListItem = CreateTodoListItem(string.Empty);
        TodoList.Add(todoListItem);
    }

    #endregion
}
